x = [1, 1, 1]
x[1] = 2

names = ['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl']
del names[1]
print(names)  # ['Alice', 'Cecil', 'Dee-Dee', 'Earl']

name = list('Perl')
print(name)  # ['P', 'e', 'r', 'l']

name[2:] = list('ar')
print(name)  # ['P', 'e', 'a', 'r']

name[1:] = list('ython')
print(name)  # ['P', 'y', 't', 'h', 'o', 'n']

# insert
numbers = [1, 5]
numbers[1:1] = [2, 3, 4]
print(numbers)  # [1, 2, 3, 4, 5]

# del
numbers[1:4] = []
print(numbers)  # [1,5]

# append
lst = [1, 2, 3]
lst.append(4)
print(lst)  # [1, 2, 3, 4]

# clear
lst.clear()
print(lst)
# lst[:] = []

# copy
a = [1, 2, 3]
a2 = a
a_copy = a.copy()
a[0] = 99
print(a, a2, a_copy)  # [99, 2, 3] [99, 2, 3] [1, 2, 3]

# count
print(a.count(2))  # 1
print(a.count(0))  # 0
print(['to', 'be', 'or', 'not', 'to', 'be'].count('to'))  # 2
x = [[1, 2], 1, 1, [2, 1, [1, 2]]]
print(x.count([1, 2]))  # 1

# extend
a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
print(a)  # [1, 2, 3, 4, 5, 6]

# +
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(a)  # [1, 2, 3]
print(c)  # [1, 2, 3, 4, 5, 6]

# index
knights = ['We', 'are', 'the', 'knights', 'who', 'say', 'ni']
print(knights.index('ni'))  # 6
print(knights[knights.index('ni')])  # ni
# print(knights.index('xxx'))  # ValueError: 'xxx' is not in list


# insert
numbers = [1, 2, 3, 5, 6, 7]
numbers.insert(3, 'four')
print(numbers)  # [1, 2, 3, 'four', 5, 6, 7]

# pop
x = [1, 2, 3]
print(x.pop())  # 3
print(x)  # [1,2]

print(x.pop(0))  # 1
print(x)  # [2]

# remove
x = ['to', 'be', 'or', 'not', 'to', 'be']
print(x.remove('be'))  # None
print(x)  # ['to', 'or', 'not', 'to', 'be']
print(x.remove('be'))  # None
print(x)  # ['to', 'or', 'not', 'to']
# print(x.remove('be'))  # ValueError: list.remove(x): x not in list

# reverse
x = [1, 2, 3]
x.reverse()
print(x)  # [3, 2, 1]

# sort
x = [4, 6, 2, 1, 7, 9]
x.sort()
print(x)  # [1, 2, 4, 6, 7, 9]

#
x = [4, 6, 2, 1, 7, 9]
y = x[:]
y.sort()
print(x, y)  # [4, 6, 2, 1, 7, 9] [1, 2, 4, 6, 7, 9]

x = [4, 6, 2, 1, 7, 9]
y = sorted(x)
print(x, y)  # [4, 6, 2, 1, 7, 9] [1, 2, 4, 6, 7, 9]

#
print(sorted('Python'))  # ['P', 'h', 'n', 'o', 't', 'y']
res = sorted('Python')
res.reverse()
print(res)  # ['y', 't', 'o', 'n', 'h', 'P']

x = ['aardvark', 'abalone', 'acme', 'add', 'aerate']
# key指定排序函数
x.sort(key=len)
print(x)

x.sort(key=len, reverse=True)
print(x)

x = [4, 6, 2, 1, 7, 9]
x.sort(reverse=True)
print(x)
